package com.hy.scope;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 自定义本地线程级别的bean作用域，不同的线程中对应的bean实例是不同的，同一个线程中同名的bean是同一个实例
 */
public class ThreadScope implements Scope {
	// 定义了作用域的名称为一个常量thread，可以在定义bean的时候给scope使用
	public static final String THREAD_SCOPE = "thread";

	// Map对象全局变量
	private ThreadLocal<Map<String, Object>> beanMap = new ThreadLocal<Map<String, Object>>() {

		@Override
		protected Map<String, Object> initialValue() {
			return new HashMap<>();
		}
	};

	@Override
	public Object get(String name, ObjectFactory<?> objectFactory) {
		Object bean = beanMap.get().get(name);
		if (Objects.isNull(bean)){
			bean = objectFactory.getObject();
			beanMap.get().put(name,bean);
		}
		return bean;
	}

	@Override
	public Object remove(String name) {
		return this.beanMap.get().remove(name);
	}

	@Override
	public void registerDestructionCallback(String name, Runnable callback) {
		// bean作用域范围结束的时候调用的方法，用于bean清理
		System.out.println("bean name clean: "+name);
	}

	@Override
	public Object resolveContextualObject(String key) {

		return null;
	}

	@Override
	public String getConversationId() {
		return Thread.currentThread().getName();
	}
}
